/*
 * 
 * Libman
 * 
 * Daniel Kaufmann, Martin Lang, Simon Moosbrugger,
 * Stefan Mayer, Stephan Halbeisen
 * 
 */
package model.dao;

import hibernate.HibernateUtil;

import java.util.List;

import model.Client;

import org.hibernate.Session;
import org.hibernate.criterion.Disjunction;
import org.hibernate.criterion.Example;
import org.hibernate.criterion.Example.CombiningMode;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;

/**
 * A DAO implementation for Client objects.
 */

public class ClientDAO extends GenericHibernateDAO<Client, Long> {
	// implementation in parent class
	/**
	 * Search client.
	 * 
	 * @param exampleClient
	 *            the example client
	 * @param maxResults
	 *            the max results
	 * @return the list
	 */
	public List<Client> searchClient(Client exampleClient, int maxResults) {
		Disjunction criterion = Restrictions.disjunction();

		Example ex = Example.create(exampleClient);
		ex.ignoreCase();
		ex.excludeZeroes();
		criterion.add(ex);
		ex.enableLike(MatchMode.ANYWHERE);
		ex.setCombiningMode(CombiningMode.Disjunction);

		Session session = HibernateUtil.currentSession();

		@SuppressWarnings("unchecked")
		List<Client> clientList = session.createCriteria(Client.class)
				.add(criterion).setMaxResults(maxResults).list();

		return clientList;
	}

	/**
	 * Search client.
	 * 
	 * @param exampleClients
	 *            the example clients
	 * @param maxResults
	 *            the max results
	 * @return the list
	 */
	public List<Client> searchClient(List<Client> exampleClients, int maxResults) {
		Disjunction criterion = Restrictions.disjunction();

		for (Client client : exampleClients) {
			Example ex = Example.create(client);
			ex.ignoreCase();
			ex.excludeZeroes();
			ex.enableLike(MatchMode.ANYWHERE);
			ex.setCombiningMode(CombiningMode.Disjunction);
			criterion.add(ex);
		}

		Session session = HibernateUtil.currentSession();

		@SuppressWarnings("unchecked")
		List<Client> clientList = session.createCriteria(Client.class)
				.add(criterion).setMaxResults(maxResults).list();

		return clientList;
	}

}